package com.th.cash.th2.main

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.th.basemodel.ext.gotoActivity
import com.th.resmodel.R
import com.th.resmodel.databinding.ViewBottomNavigationBinding
import com.th.cash.storage.LocalSetting
import com.th.cash.th2.login.LoginActivity

class BottomNavigationView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : LinearLayout(context, attrs, defStyle) {

    private val binding =
        ViewBottomNavigationBinding.inflate(LayoutInflater.from(context), this, true)
    private var currentPosition = 0
    private var onTabSelectedListener: ((Int) -> Unit)? = null

    private val tabIcons = arrayOf(
        R.mipmap.img_43 to R.mipmap.img_17,
        R.mipmap.img_44 to R.mipmap.img_41,
        R.mipmap.img_45 to R.mipmap.img_42
    )

    private val tabTexts = arrayOf(
        "เงินกู้ ",
        "ข้อมูลพื้นฐาน",
        "ศูนย์ส่วนบุคคล"
    )

    init {
        initViews()
        setupClickListeners()
    }

    private fun initViews() {
        // 初始化所有tab的图标和文字
        with(binding) {
            ivTab1.setImageResource(tabIcons[0].first)
            ivTab2.setImageResource(tabIcons[1].first)
            ivTab3.setImageResource(tabIcons[2].first)

            tvTab1.text = tabTexts[0]
            tvTab2.text = tabTexts[1]
            tvTab3.text = tabTexts[2]
        }
        updateTabState(0)
    }

    private fun setupClickListeners() {
        with(binding) {
            llTab1.setOnClickListener { selectTab(0) }
            llTab2.setOnClickListener { selectTab(1) }
            llTab3.setOnClickListener { selectTab(2) }
        }
    }

    private fun selectTab(position: Int) {
        if ((position == 1 || position == 2) && !LocalSetting.userLogin) {
            gotoActivity<LoginActivity>()
            return
        }

        if (position != currentPosition) {
            updateTabState(position)
            onTabSelectedListener?.invoke(position)
        }
    }

    fun setCurrentActiveItem(position: Int) {
        if (position != currentPosition) {
            updateTabState(position)
        }
    }

    private fun updateTabState(position: Int) {
        // 重置所有tab为未选中状态
        with(binding) {
            ivTab1.setImageResource(tabIcons[0].first)
            ivTab2.setImageResource(tabIcons[1].first)
            ivTab3.setImageResource(tabIcons[2].first)

            tvTab1.setTextColor(ContextCompat.getColor(context, R.color.c_C6CACB))
            tvTab2.setTextColor(ContextCompat.getColor(context, R.color.c_C6CACB))
            tvTab3.setTextColor(ContextCompat.getColor(context, R.color.c_C6CACB))
        }

        // 设置选中的tab
        when (position) {
            0 -> {
                binding.ivTab1.setImageResource(tabIcons[0].second)
                binding.tvTab1.setTextColor(ContextCompat.getColor(context, R.color.c_407EF6))
            }

            1 -> {
                binding.ivTab2.setImageResource(tabIcons[1].second)
                binding.tvTab2.setTextColor(ContextCompat.getColor(context, R.color.c_407EF6))
            }

            2 -> {
                binding.ivTab3.setImageResource(tabIcons[2].second)
                binding.tvTab3.setTextColor(ContextCompat.getColor(context, R.color.c_407EF6))
            }
        }

        currentPosition = position
    }

    fun setOnTabSelectedListener(listener: (Int) -> Unit) {
        onTabSelectedListener = listener
    }
} 